package com.ijianji.module_zhuanzhu.model

import androidx.lifecycle.MutableLiveData
import com.fenghuajueli.libbasecoreui.utils.NumsUtils
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModel2
import com.ijianji.module_zhuanzhu.utils.TimeUtils
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.math.BigDecimal
import java.math.RoundingMode
import java.util.concurrent.TimeUnit

/**
 *===============================================
 * Create by kongbaige on 2021/5/8
 * Email 1531603384@qq.com
 *===============================================
 */
class ZhuanZhuModel : BaseViewModel2() {

    val timeShowData = MutableLiveData<String>()

    val progressData = MutableLiveData<Int>()

    //默认选中第一个 普通专注
    var currentTypePosition: Int = 0

    //默认选中第一个 25min
    var currentTimePosition: Int = 0

    var disposable: Disposable? = null

    //总的时间
    var duration: Int = 0

    //当前已经执行的时间
    var currentDuration: Int = 0

    //是否计时完成
    var isEndTime: Boolean = false

    var timeModel: Int = 0


    fun getProgress(): Int {
        when (currentTimePosition) {
            0 -> {
                return 25
            }
            1 -> {
                return 60
            }
            2 -> {
                return 120
            }
            3 -> {
                return -1
            }
            else -> {
                return -1
            }
        }
    }


    /**
     * @param time 秒数
     */
    fun startDownTime(model: Int, time: Int, isFirst: Boolean) {
        if (isFirst) {
            this.timeModel = model
            this.duration = time
            this.currentDuration = duration
        }else{
            if (model!=3) {
                this.timeModel = model
                this.duration = time
                this.currentDuration = duration
            }
        }
        disposable?.dispose()
        Observable.interval(1000, 1000, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(object : Observer<Long> {
                    override fun onSubscribe(d: Disposable) {
                        disposable = d
                    }

                    override fun onNext(t: Long) {
                        if (timeModel != 3) {
                            if (currentDuration > 0) {
                                currentDuration--
                                val date = TimeUtils.msecToTime(currentDuration * 1000)
                                val process: Int = (formatTwoDecimal((duration - currentDuration) / duration.toDouble()) * 100).toInt()
                                progressData.postValue(process)
                                timeShowData.postValue(date)
                            } else {
                                //执行完毕了
                                isEndTime = true
                                progressData.postValue(100)
                                disposable?.dispose()
                            }
                        } else {
                            currentDuration--
                            val seconds = duration - currentDuration
                            val date = TimeUtils.msecToTime(seconds * 1000)
                            timeShowData.postValue(date)
                        }
                    }

                    override fun onError(e: Throwable) {

                    }

                    override fun onComplete() {

                    }
                })
    }


    fun resumeTime() {
        startDownTime(timeModel, currentDuration,false)
    }

    fun stopTime() {
        disposable?.dispose()
    }


    fun formatTwoDecimal(num: Double): Double {
        val bg = BigDecimal(num).setScale(2, RoundingMode.HALF_UP)
        return bg.toDouble()
    }
}